﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using YouFood.ViewModel;
using YouFood.Web;
using YouFood.Web.Model;

namespace YouFood

{
    public partial class WaiterView : Page, INotifyPropertyChanged
    {
        #region property

        private ObservableCollection<OrderSubViewModel> _ordersCooked;
        public ObservableCollection<OrderSubViewModel> OrdersCooked
        {
            get { return _ordersCooked; }
            set
            {
                _ordersCooked = value;
                NotifyPropertyChanged("OrdersCooked");
            }
        }

        private ObservableCollection<OrderSubViewModel> _callWaiter;
        public ObservableCollection<OrderSubViewModel> CallWaiter
        {
            get { return _callWaiter; }
            set
            {
                _callWaiter = value;
                NotifyPropertyChanged("CallWaiter");
            }
        }

        private OrderSubViewModel _orderCookedSelected;
        public OrderSubViewModel OrderCookedSelected
        {
            get { return _orderCookedSelected; }
            set
            {
                _orderCookedSelected = value;
                NotifyPropertyChanged("OrderCookedSelected");
            }
        }

        private OrderSubViewModel _callWaiterSelected;
        public OrderSubViewModel CallWaiterSelected
        {
            get { return _callWaiterSelected; }
            set
            {
                _callWaiterSelected = value;
                NotifyPropertyChanged("CallWaiterSelected");
            }
        }

        private OrderCookerDomainContext _context = new OrderCookerDomainContext();

        #endregion

        public WaiterView()
        {
            if (!WebContext.Current.User.IsInRole("Serveur"))
                return;

            InitializeComponent();

            MyImage.Source = new BitmapImage(new Uri("/YouFood;component/Assets/Images/Fleche.png", UriKind.RelativeOrAbsolute));
            MyImage2.Source = new BitmapImage(new Uri("/YouFood;component/Assets/Images/Fleche.png", UriKind.RelativeOrAbsolute));
            OrdersCooked = new ObservableCollection<OrderSubViewModel>();

            DataContext = this;
            RefreshList();
        }

        private void RefreshList()
        {
            var query = _context.GetOrdersWaiterQuery();
            var loadData = _context.Load(query);
            loadData.Completed += new EventHandler(loadData_Completed);
        }

        void loadData_Completed(object sender, EventArgs e)
        {
            var senders = (LoadOperation<Order>)sender;

            OrdersCooked.Clear();

            OrdersCooked = new ObservableCollection<OrderSubViewModel>(senders.Entities.Where(x => x.Status == 2)
                .Select(item => new OrderSubViewModel(item))
                .OrderBy(x => x.OrderLocal.Date));

            CallWaiter = new ObservableCollection<OrderSubViewModel>(senders.Entities.Where(x => x.Status == 10)
                .Select(item => new OrderSubViewModel(item))
                .OrderBy(x => x.OrderLocal.Date));
        }

        #region INotify
        
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(OrderCookedSelected != null)
            {
                OrderCookedSelected.OrderLocal.Status = 3;
                _context.SubmitChanges();
                OrdersCooked.Remove(OrderCookedSelected);
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (CallWaiterSelected != null)
            {
                CallWaiterSelected.OrderLocal.Status = 20;
                _context.SubmitChanges();
                CallWaiter.Remove(CallWaiterSelected);
            }
        }
    }
}
